package com.lienlq.voicemusic.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.lienlq.voicemusic.R;
import com.lienlq.voicemusic.entity.Composer;
import com.lienlq.voicemusic.xmlprc.XMLRPCMethod;
import com.lienlq.voicemusic.xmlprc.XMLRPCMethodCallback;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ComposerFragment extends Fragment{
	private ListView listview = null;
	private ArrayList<Composer> ComposerList = new ArrayList<Composer>();
	JSONObject jsObject;
	Object result;
	JSONArray jsArray;
	Context context;
	ArrayAdapter adapter=null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		XMLRPCMethod method=new XMLRPCMethod("selectAllComposer", new XMLRPCMethodCallback() {
			
			@Override
			public void CallFinished(Object result) {
				// TODO Auto-generated method stub
				try{
					jsObject=new JSONObject(result.toString());
					jsArray=jsObject.getJSONArray("composer");
					for(int i=0;i<jsArray.length();i++)
					{
						JSONObject obj=jsArray.getJSONObject(i);
						Composer composer=new Composer(obj.getString("name"));
						ComposerList.add(composer);
					}
					adapter=new CompserAdapter();
					listview.setAdapter(adapter);
				}catch(Exception e)
				{
					
				}
			}
		});
		Object[] params=null;
		method.call(params);
		
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		//loadContent();
		View v = inflater.inflate(R.layout.fragment_composer, container, false);
        listview = (ListView)v.findViewById(R.id.composers_list_fragment_composer);
        context=getActivity();
		return v;
	}

	private void loadContent() {
		
						
	}
	
	class CompserAdapter extends ArrayAdapter<Composer>
	{
		public CompserAdapter()
		{
			super(context,R.layout.item_composer_fragment_composer,ComposerList);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View row=convertView;
			ViewHolder holder=null;
			if(row==null)
			{
				LayoutInflater inflater=getActivity().getLayoutInflater();
				row=inflater.inflate(R.layout.item_composer_fragment_composer,parent,false);
				holder=new ViewHolder(row);
				row.setTag(holder);
			}
			else
			{
				holder=(ViewHolder)row.getTag();
			}
			holder.populateFrom(ComposerList.get(position));
			return row;
		}
		
	}
	class ViewHolder{
		TextView tvComposerName;
		
		public ViewHolder(View row)
		{
			tvComposerName=(TextView)row.findViewById(R.id.item_composer);
		}
		void populateFrom(Composer composer)
		{
			tvComposerName.setText(composer.ComposerName);
		}
	}
}
